﻿using System;
using UnityEngine;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000B2F RID: 2863
	internal class XGyroscope : XSingleton<XGyroscope>
	{
		// Token: 0x1700300C RID: 12300
		// (get) Token: 0x0600A79E RID: 42910 RVA: 0x001DB01C File Offset: 0x001D921C
		public float Scale
		{
			get
			{
				return this._scale;
			}
		}

		// Token: 0x1700300D RID: 12301
		// (get) Token: 0x0600A79F RID: 42911 RVA: 0x001DB034 File Offset: 0x001D9234
		public float DeadZone
		{
			get
			{
				return this._deadzone;
			}
		}

		// Token: 0x1700300E RID: 12302
		// (get) Token: 0x0600A7A0 RID: 42912 RVA: 0x001DB04C File Offset: 0x001D924C
		public float Frequency
		{
			get
			{
				return this._frequency;
			}
		}

		// Token: 0x1700300F RID: 12303
		// (get) Token: 0x0600A7A1 RID: 42913 RVA: 0x001DB064 File Offset: 0x001D9264
		// (set) Token: 0x0600A7A2 RID: 42914 RVA: 0x001DB07C File Offset: 0x001D927C
		public bool Enabled
		{
			get
			{
				return this._enabled;
			}
			set
			{
				bool flag = this._enabled != value;
				if (flag)
				{
					this._touch_count_this_frame = 0;
					this._enabled = value;
				}
				Input.gyro.enabled = this._enabled;
				bool enabled = this._enabled;
				if (enabled)
				{
					this._scale = (XSingleton<XUpdater.XUpdater>.singleton.EditorMode ? 1f : XSingleton<XGlobalConfig>.singleton.GyroScale);
					this._deadzone = XSingleton<XGlobalConfig>.singleton.GyroDeadZone;
					this._frequency = XSingleton<XGlobalConfig>.singleton.GyroFrequency;
					Input.gyro.updateInterval = 1f / this._frequency;
				}
				this.Cancel();
				XSingleton<XGesture>.singleton.Cancel();
			}
		}

		// Token: 0x17003010 RID: 12304
		// (get) Token: 0x0600A7A3 RID: 42915 RVA: 0x001DB134 File Offset: 0x001D9334
		public int touchCount
		{
			get
			{
				return this._touch_count_this_frame;
			}
		}

		// Token: 0x0600A7A4 RID: 42916 RVA: 0x001DB14C File Offset: 0x001D934C
		public void OnEnterScene()
		{
			XOptionsDocument specificDocument = XDocuments.GetSpecificDocument<XOptionsDocument>(XOptionsDocument.uuID);
			bool flag = specificDocument != null;
			if (flag)
			{
				this.Enabled = (specificDocument.GetValue(XOptionsDefine.OD_Gyro) != 0);
			}
		}

		// Token: 0x0600A7A5 RID: 42917 RVA: 0x001DB184 File Offset: 0x001D9384
		public void Update()
		{
			this._touch_count_this_frame = 0;
			bool currentPos = this.GetCurrentPos();
			if (currentPos)
			{
				this._touches.faketouch.fingerId = XFastEnumIntEqualityComparer<XFingerId>.ToInt(XFingerId.XGyroscope_0);
				this._touches.faketouch.position = XGyroscope._curr_pos;
				this._touches.faketouch.deltaTime = Time.smoothDeltaTime;
				this._touches.faketouch.deltaPosition = XGyroscope._curr_pos - XGyroscope._last_pos;
				this._touches.faketouch.phase = ((XGyroscope._curr_pos == XGyroscope._init_pos) ? (TouchPhase)3 : ((XGyroscope._last_pos == XGyroscope._init_pos) ? (TouchPhase)0 : (TouchPhase)1));
				this._touches.faketouch.tapCount = 1;
				this._touches.Fake = true;
				XGyroscope._last_pos = XGyroscope._curr_pos;
				this._touch_count_this_frame++;
			}
		}

		// Token: 0x0600A7A6 RID: 42918 RVA: 0x001DB27C File Offset: 0x001D947C
		public XTouchItem GetTouch(int idx)
		{
			return this._touches;
		}

		// Token: 0x0600A7A7 RID: 42919 RVA: 0x001DB294 File Offset: 0x001D9494
		public void Set(float scale, float times, float deadzone)
		{
			bool enabled = this.Enabled;
			if (enabled)
			{
				this._scale = (XSingleton<XUpdater.XUpdater>.singleton.EditorMode ? 1f : scale);
				this._deadzone = deadzone;
				this._frequency = times;
				Input.gyro.updateInterval = 1f / this._frequency;
			}
		}

		// Token: 0x0600A7A8 RID: 42920 RVA: 0x001DB2F0 File Offset: 0x001D94F0
		private bool GetCurrentPos()
		{
			float num = 0f;
			float num2 = 0f;
			bool editorMode = XSingleton<XUpdater.XUpdater>.singleton.EditorMode;
			if (editorMode)
			{
				bool key = Input.GetKey((KeyCode)276);
				if (key)
				{
					num -= 1f;
				}
				bool key2 = Input.GetKey((KeyCode)275);
				if (key2)
				{
					num += 1f;
				}
				bool key3 = Input.GetKey((KeyCode)274);
				if (key3)
				{
					num2 -= 1f;
				}
				bool key4 = Input.GetKey((KeyCode)273);
				if (key4)
				{
					num2 += 1f;
				}
			}
			else
			{
				Vector3 rotationRateUnbiased = Input.gyro.rotationRateUnbiased;
				rotationRateUnbiased.z = 0f;
				bool flag = rotationRateUnbiased.sqrMagnitude < this._deadzone * this._deadzone;
				if (flag)
				{
					num2 = (num = 0f);
				}
				else
				{
					num = -(rotationRateUnbiased.y * 57.29578f);
					num2 = rotationRateUnbiased.x * 57.29578f;
				}
			}
			bool flag2 = num != 0f || num2 != 0f;
			bool result;
			if (flag2)
			{
				XGyroscope._curr_pos = XGyroscope._last_pos + new Vector2(this._scale * num, this._scale * num2);
				result = true;
			}
			else
			{
				bool flag3 = XGyroscope._curr_pos != XGyroscope._init_pos;
				if (flag3)
				{
					XGyroscope._last_pos = XGyroscope._init_pos;
					XGyroscope._curr_pos = XGyroscope._init_pos;
					result = true;
				}
				else
				{
					result = false;
				}
			}
			return result;
		}

		// Token: 0x0600A7A9 RID: 42921 RVA: 0x001DB458 File Offset: 0x001D9658
		public void Cancel()
		{
			XGyroscope._last_pos = XGyroscope._init_pos;
			XGyroscope._curr_pos = XGyroscope._init_pos;
		}

		// Token: 0x04003E01 RID: 15873
		private XTouchItem _touches = new XTouchItem();

		// Token: 0x04003E02 RID: 15874
		private bool _enabled = false;

		// Token: 0x04003E03 RID: 15875
		private static Vector2 _init_pos = new Vector2((float)Screen.width * 0.55f, (float)Screen.height * 0.55f);

		// Token: 0x04003E04 RID: 15876
		private static Vector2 _last_pos = XGyroscope._init_pos;

		// Token: 0x04003E05 RID: 15877
		private static Vector2 _curr_pos = XGyroscope._init_pos;

		// Token: 0x04003E06 RID: 15878
		private int _touch_count_this_frame = 0;

		// Token: 0x04003E07 RID: 15879
		private float _frequency = 30f;

		// Token: 0x04003E08 RID: 15880
		private float _scale = 0.2f;

		// Token: 0x04003E09 RID: 15881
		private float _deadzone = 0.01f;
	}
}
